package com.biubiu.newapplication.activity.module

import android.os.Bundle
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.biubiu.newapplication.R
import com.biubiu.newapplication.activity.data.DataProvider
import com.biubiu.newapplication.activity.vm.BodyVm
import com.biubiu.newapplication.activity.vm.BottomVm
import com.biubiu.newapplication.activity.vm.TopVm
import com.biubiu.newapplication.base.BaseActivity
import com.biubiu.newapplication.databinding.ActivityUniversalAdapterBinding
import com.biubiu.universaladapter.RecyclerViewAdapter

class UniversalAdapterActivity: BaseActivity<ActivityUniversalAdapterBinding>() {
    private var toast: Toast? = null
    private lateinit var mTopVm: TopVm
    private lateinit var mBottomVm: BottomVm
    private lateinit var mBodyVm: BodyVm
    override val layoutId: Int
        get() = R.layout.activity_universal_adapter

    override fun initView(savedInstanceState: Bundle?) {
        createViewModules()
        mDataBinding.apply {
            recyclerView.layoutManager = LinearLayoutManager(this@UniversalAdapterActivity)
            recyclerView.adapter = RecyclerViewAdapter(true, mTopVm, mBodyVm, mBottomVm)
        }
    }

    private fun createViewModules() {
        mTopVm = TopVm()
        mBottomVm = BottomVm()
        mBodyVm = BodyVm().apply {
            setList(DataProvider.getBodyList())
        }
        mBodyVm.setOnModuleItemClickListener { data, dataPosition, adapterPosition ->
            showToast(data.title)
        }
    }

    private fun showToast(msg: String){
        toast?.cancel()
        toast = Toast.makeText(this@UniversalAdapterActivity, msg, Toast.LENGTH_SHORT)
        toast?.show()
    }
}